package com.gavindon.android.view.start

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import androidx.lifecycle.ViewModelProviders
import com.gavindon.android.HomeActivity
import com.gavindon.android.base.SP_TOKEN
import com.gavindon.android.entity.LoginStatus
import com.gavindon.android.network.DataResult
import com.gavindon.android.utils.SpUtils
import com.gavindon.android.viewmodel.HomeViewModel
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast

/**
 * description: android 冷启动时白屏窗口 设置theme
 * Created by liNan on  2022-2-8 17:21
 */
class PreviewWindow : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        /*    *//*延时3s 进入主页*//*
        Handler(Looper.getMainLooper()).postDelayed(
            {
                startActivity<HomeActivity>()
                this.finish()
            }, 3
        )*/
        hasLogin()
    }

    private fun hasLogin() {
        val homeVm = ViewModelProviders.of(this).get(HomeViewModel::class.java)
        val token = SpUtils.get(SP_TOKEN, "")
        LoginStatus.token = token
        homeVm.getUserInfo().observe(this) {
            if (it is DataResult.Success) {
                LoginStatus.hasLogin = true
                LoginStatus.userInfoResp = it.data
                startActivity<HomeActivity>()
                this.finish()
            } else {
                LoginStatus.hasLogin = false
                startActivity<HomeActivity>()
                this.finish()
            }
        }
    }

}